/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.server;

import java.util.List;
import java.util.Observable;
import java.util.Observer;
import uvexposureapplication.rpc.AbstractPseudoRPCServerStub;
import uvexposureapplication.rpc.servers.PreferenceRepositoryPseudoRPCServerStub;
import uvexposureapplication.server.preferencerepository.UserData;
import uvexposureapplication.utils.IOStream;

/**
 *
 * @author matthieukluj
 */
public class PreferenceRepository implements Observer {

    public List<UserData> userDatas;
    public AbstractPseudoRPCServerStub preferenceRepositoryPseudoRPCServerStub;
    public AbstractPseudoRPCServerStub preferenceRepositoryPseudoRPCServerStub2;
    public AbstractPseudoRPCServerStub preferenceRepositoryPseudoRPCServerStub3;
    public Thread a, b, c;

    public PreferenceRepository(String userDataFile, String server) {
        this.userDatas = IOStream.readUsersData(userDataFile);

        this.preferenceRepositoryPseudoRPCServerStub = new PreferenceRepositoryPseudoRPCServerStub(this, "fetchUserPreference", server);
        a = new Thread(this.preferenceRepositoryPseudoRPCServerStub);
        a.start();

        this.preferenceRepositoryPseudoRPCServerStub2 = new PreferenceRepositoryPseudoRPCServerStub(this, "fetchUserPreferenceTemp", server);
        b = new Thread(this.preferenceRepositoryPseudoRPCServerStub2);
        b.start();

        this.preferenceRepositoryPseudoRPCServerStub3 = new PreferenceRepositoryPseudoRPCServerStub(this, "fetchUserPreferenceUV", server);
        c = new Thread(this.preferenceRepositoryPseudoRPCServerStub3);
        c.start();
    }

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("usage: java PreferenceRepository [preference-file][ElvinURL]");
            System.exit(0);
        }

        PreferenceRepository pm = new PreferenceRepository(args[0], args[1]);
    }

    public String fetchUserPreference() {
        String userDataFile = "";
        for (UserData userData : this.userDatas) {
            userDataFile += "name:" + userData.getName() + ":";
            userDataFile += "Skin Type:" + userData.getSkinType() + ":";
            userDataFile += "Temp:" + userData.getTempThreshold() + "-";
        }
        System.out.println("[SENDING] Initial User Preference TO ContextManager");
        return userDataFile;
    }

    @Override
    public void update(Observable o, Object o1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String fetchUVPref(String parameters) {
        String uvPref = "";
        int found = 0;
        for (UserData userData : this.userDatas) {
            if (userData.getName().equals(parameters)) {
                uvPref = userData.getName() + ":" + userData.getSuggestionUV();
                if (!userData.getSuggestionUV().equals("")) {
                    found = 1;
                }
            }
        }
        if (found == 0) {
            uvPref += "No UV Suggestion Found";
        }
        System.out.println("[SENDING] UV Preference TO ContextManager");
        return uvPref;
    }

    public String fetchTempPref(String parameters) {
        String uvPref = "";
        int found = 0;
        for (UserData userData : this.userDatas) {
            if (userData.getName().equals(parameters)) {
                uvPref = userData.getName() + ":" + userData.getSuggestionTemp();
                if (!userData.getSuggestionUV().equals("")) {
                    found = 1;
                }
            }
        }
        if (found == 0) {
            uvPref += "No Temp Suggestion Found";
        }
        System.out.println("[SENDING] Temp Preference TO ContextManager");
        return uvPref;
    }

    public void shutDown() {
        a.interrupt();
        b.interrupt();
        c.interrupt();
        System.exit(0);
    }
}
